package gr.aueb.mscis.chord;

import gr.aueb.mscis.Activities.ContentNodesActivity;
import gr.aueb.mscis.protocol.NodeIdentifier;
import gr.aueb.mscis.protocol.ProtocolType;
import gr.aueb.mscis.protocol.RemoteMessage;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

/*pros to paron to service kanei tin sundesi me ton tracker*/
public class ChordService extends Service {

	private NodeIdentifier predecessor;
	private NodeIdentifier successor;
	
	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onCreate() {
	}

	@Override
	public void onDestroy() {
	}

	@Override
	public void onStart(Intent intent, int startid) {
		register();
	}

	public void setPredecessor(NodeIdentifier predecessor){
		this.predecessor = predecessor;
	}
	
	public NodeIdentifier getPredecessor(){
		NodeIdentifier p = new NodeIdentifier();
		p = this.predecessor;
		return p;
	}
	
	public void setSuccessor(NodeIdentifier successor){
		this.successor = successor;
	}
	
	public NodeIdentifier getSuccessor(){
		NodeIdentifier s = new NodeIdentifier();
		s = this.successor;
		return s;
	}
	
	/* register tou node sto tracker */
	private void register() {
		RemoteMessageClient connect = new RemoteMessageClient();
		RemoteMessage response = connect.register();
		RemoteMessageServer remoteServer = null;
		
		/*emfanizei ston node ti minima elave apo ton tracker*/
		if (response.getProtocolType().equals(ProtocolType.CREATE_CHORD)) {
			ContentNodesActivity.statusText.setText("Received: create");
			
			/*TREXEI SERVER GIA SYNDESH ME ALLOUS KOMVOUS*/
			remoteServer = new RemoteMessageServer();
			remoteServer.start();
			
			setSuccessor(response.getNodeId());
			setPredecessor(null);
			/*PREPEI NA GINEI REDISTRIBUTE KEYS*/ 
			//einai ypeythynos gia ola ta keys, ara de tha ginei se ayth th fash
			
		} else if (response.getProtocolType().equals(ProtocolType.JOIN_CHORD)) {
			ContentNodesActivity.statusText.setText("Received: join");
			
			ContentNodesActivity.statusText.setText("Informing Successor");

			remoteServer = new RemoteMessageServer();
			remoteServer.start();
			
			NodeIdentifier n = (NodeIdentifier) response.getPayload();
			setPredecessor(null);
			//no. o tracker dn kserei ton successor, alla enan komvo pou tha
			//mas pei ton successor mas
			setSuccessor(n);
			
			/*Enhmerwnoume ton successor oti eimaste o prohgoumenos tou*/
			RemoteMessageClient suc = new RemoteMessageClient();
			//no. h findSuccessor vriskei ton successor enos hash. episis, de tha
			//ginei k ayto k to panw. mono to find_successor.
			suc.findSuccessor(n.getAddr(), n.getPort());
			
			/*Me thn periodikh ananewsh tou daxtyliou tha klithoume apo ton predecessor mas kai tha ton prosthesoume*/
			
			/*Redistribute keys*/
			
		}else if (response.getProtocolType().equals(ProtocolType.ALREADY_IN_RING)) {
			ContentNodesActivity.statusText.setText("Received: already in ring");
			/*do nothing*/
		}
		
		//Den kserw ti prospatheis na kaneis, alla kanei apeiro loop k kollaei
//		while(true){
//			if(remoteServer.getResult() != null){
//				setPredecessor(remoteServer.getResult());
//				System.out.println("GOT SUCCESSOR: "+predecessor.getAddr());
//			}
//		}
	}
}
